Repository navigation
Jpeg: streaming encode for baseline with the standard tables - #11
Merged
Merged
Conversation
JpegImageEncoder.Create now returns a StreamingEncoderSession for every option set except OptimizeHuffman / Progressive: the headers go out at AddFrame and each MCU row (8 rows, 16 for 4:2:0) is transformed, coded and handed to the writer as soon as its last row arrives. Memory is one MCU row of pixels and output. The transform loop moves into McuRowEncoder, which both the buffered JpegEncoder.Encode and the session drive, so the bytes are identical for the same pixels and options, however rows are split. OptimizeHuffman / Progressive keep the buffered session (IsStreaming false). The encode benchmark gains a "streamed" contestant (16-row bands) that must produce JpegEncoder.Encode's bytes before it is timed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ecord timings A writer failure while the headers or the EOI trailer go out left the session looking complete: a retried AddFrame wrote the headers twice, and Commit after a failed trailer succeeded with a truncated file. Both now mark the session faulted, and Finish throws. The JPEG benchmark README records streamed vs buffered encode time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JPEG streaming encode (unit 5 of the streaming batch).
What changes
JpegImageEncoder.Createreturns aStreamingEncoderSessionfor baseline encoding with the Annex K tables, which covers everyJpegEncodeOptionsfield exceptOptimizeHuffmanandProgressive(Quality, Subsampling, RestartInterval, Exif, Xmp, IccProfile and frame metadata all stream). The headers go out atAddFrame. Each MCU row (8 rows, or 16 for 4:2:0) is converted, transformed, coded and flushed to the writer as soon as its last row arrives, and EOI is written when the last row is in.OptimizeHuffman/Progressivekeep today's buffered session (IsStreaming == false) and produce unchanged bytes.JpegEncoder.Transform's loop body moved intoMcuRowEncoder, which holds the state one MCU row passes to the next (DC predictors, restart count).JpegEncoder.Encodeand the session drive the same code, so the streamed bytes are identical toJpegEncoder.Encode's for the same pixels and options, howeverWriteRowsis chunked. The arithmetic and kernels are untouched.[0, Position)of the bit writer is final, because every MCU ends withStuff()and markers end on a byte boundary. The session writes those bytes out and resetsPosition/Markto 0. The pending bits (fewer than 8) stay in the accumulator and the next store rewrites them at offset 0.WriteRows/AddFrame/ trailer that fails part way (cancellation, or a writer exception) leaves the session faulted. Later calls andCommitthrowInvalidOperationException, so the session never writes a corrupt or truncated file silently.AddFrameinstead of atCommitfor streaming sessions.Tests (
JpegStreamingEncodeTests)JpegEncoder.Encodefor:PixelFormat;StreamingAssert.EncoderEmitsBeforeCommit, plus a check that the first MCU row is emitted exactly when its 16th row arrives.AllocatesLessThan(1 MiB)for an 8192×8192 frame fed in 64-row bands.IsStreamingis false for optimised / progressive sessions, and their bytes are unchanged.WriteRowscall, and a writer failure at the trailer makingCommitfail (checked by breaking the code: the test fails without the fix).FileStreamthat is flushed on every write. The file is on disk after the first band. Decoded with our decoder and with Magick.NET, it equals the buffered encode's decode, and the bytes are identical.Runs: full suite 7063 passed / 0 failed (559 skipped: HEIF, OpenJPEG and webp oracle tools are not installed in this worktree). The JPEG classes with
REQUIRE_ORACLES=1: 777 passed. The*Jpeg*tests withDOTNET_EnableAVX2=0and withDOTNET_EnableHWIntrinsic=0: 0 failures.Benchmarks
Ryzen 7 5800X, Windows 11 (10.0.26200), .NET 10.0.15, AVX2 (no AVX-512). Other jobs were sharing the machine.
encode --mode all --gate --baseline: gate passed. There is no regression of the buffered encode against main 533b03e in any mode; the differences are within run-to-run noise.streamedcontestant (16-row bands; the run aborts unless its bytes equalours) is within 1.00 to 1.01× of the buffered time at q75 to q95. At q50 4:2:0 it measured 1.05× in one run and 0.99× in another.The numbers are in
benchmarks/Sdcb.SimdCodecs.Jpeg.Benchmarks/README.md. The decodeab(BDN) was not run: it measures the decoder, which this change does not touch. The encoder's same-process A/B isencode --baseline, shown above.Needs outside this unit
None for Abstractions or the facade.
🤖 Generated with Claude Code